#!/bin/bash
# Tomcat WAR包更新脚本
# 用法: ./update_war.sh [WAR文件路径] [应用名称]

set -e  # 遇到错误立即退出

# 参数检查
if [ $# -ne 2 ]; then
  echo "用法: $0 [WAR文件路径] [应用名称]"
  echo "示例: $0 /tmp/myapp-1.2.0.war myapp"
  exit 1
fi

WAR_FILE=$1
APP_NAME=$2
TIMESTAMP=$(date +%Y%m%d%H%M%S)
TOMCAT_HOME="/opt/tomcat"  # 根据实际修改
BACKUP_DIR="/opt/backup/tomcat"  # 根据实际修改
TOMCAT_USER="tomcat"  # 根据实际修改
LOG_FILE="/opt/log/tomcat_update.log"

# 创建日志文件和备份目录
mkdir -p "$BACKUP_DIR"
touch "$LOG_FILE"
chown $TOMCAT_USER:$TOMCAT_USER "$LOG_FILE"

echo "==============================================" >> "$LOG_FILE"
echo "$(date) - 开始更新 $APP_NAME" >> "$LOG_FILE"

# 1. 验证WAR文件
if [ ! -f "$WAR_FILE" ]; then
  echo "错误: WAR文件 $WAR_FILE 不存在" | tee -a "$LOG_FILE"
  exit 1
fi

if ! unzip -t "$WAR_FILE" >/dev/null 2>&1; then
  echo "错误: WAR文件 $WAR_FILE 损坏" | tee -a "$LOG_FILE"
  exit 1
fi

# 2. 备份当前应用
echo "$(date) - 备份当前应用..." >> "$LOG_FILE"
mkdir -p "$BACKUP_DIR/$TIMESTAMP"
cp "$TOMCAT_HOME/webapps/$APP_NAME.war" "$BACKUP_DIR/$TIMESTAMP/" 2>/dev/null || :
cp -r "$TOMCAT_HOME/webapps/$APP_NAME" "$BACKUP_DIR/$TIMESTAMP/" 2>/dev/null || :

# 3. 停止Tomcat
echo "$(date) - 停止Tomcat服务..." >> "$LOG_FILE"
if pgrep -f tomcat >/dev/null; then
  $TOMCAT_HOME/bin/shutdown.sh >> "$LOG_FILE" 2>&1
  sleep 10

  # 强制停止如果正常停止失败
  if pgrep -f tomcat >/dev/null; then
    echo "$(date) - 正常停止失败，尝试强制停止..." >> "$LOG_FILE"
    pkill -9 -f tomcat >> "$LOG_FILE" 2>&1
    sleep 5
  fi
fi

# 4. 清理旧文件
echo "$(date) - 清理旧文件..." >> "$LOG_FILE"
rm -f "$TOMCAT_HOME/webapps/$APP_NAME.war"
rm -rf "$TOMCAT_HOME/webapps/$APP_NAME"
rm -rf "$TOMCAT_HOME/work/Catalina/localhost/$APP_NAME"

# 5. 部署新WAR
echo "$(date) - 部署新WAR文件..." >> "$LOG_FILE"
cp "$WAR_FILE" "$TOMCAT_HOME/webapps/$APP_NAME.war"
chown $TOMCAT_USER:$TOMCAT_USER "$TOMCAT_HOME/webapps/$APP_NAME.war"

# 6. 启动Tomcat
echo "$(date) - 启动Tomcat服务..." >> "$LOG_FILE"
sudo -u $TOMCAT_USER "$TOMCAT_HOME/bin/startup.sh" >> "$LOG_FILE" 2>&1

# 7. 验证部署
echo "$(date) - 验证部署..." >> "$LOG_FILE"
sleep 10  # 等待应用启动

# 检查进程
if ! pgrep -f tomcat >/dev/null; then
  echo "$(date) - 错误: Tomcat未启动" | tee -a "$LOG_FILE"
  exit 1
fi

# 检查日志错误
ERRORS=$(grep -i "ERROR" "$TOMCAT_HOME/logs/catalina.out" | tail -n 5)
if [ -n "$ERRORS" ]; then
    echo "$(date) - 检测到启动错误，请检查日志：" >> "$LOG_FILE"
    echo "$ERRORS" >> "$LOG_FILE"
fi

# 简单应用健康检查（根据应用实际情况修改）
#HEALTH_CHECK_URL="http://localhost:8080/$APP_NAME/health"
#echo "$(date) - 开始健康检查，URL: $HEALTH_CHECK_URL" >> "$LOG_FILE"
#if command -v curl &>/dev/null; then
##  if curl -s --max-time 10 "$HEALTH_CHECK_URL" | grep -q '"status":"UP"'; then
#  if curl -s --max-time 10 "$HEALTH_CHECK_URL" | grep -q "UP"; then
#    echo "$(date) - 应用健康检查通过" >> "$LOG_FILE"
#  else
#    echo "$(date) - 错误: 应用健康检查失败" | tee -a "$LOG_FILE"
#    exit 1
#  fi
#fi

echo "$(date) - 更新成功完成!" | tee -a "$LOG_FILE"
echo "==============================================" >> "$LOG_FILE"

# su: warning: cannot change directory to /home/tomcat: No such file or directory
#sudo mkdir -p /home/tomcat
#sudo chown tomcat:tomcat /home/tomcat
#sudo chmod 750 /home/tomcat
